//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "AnalyticGJRGARCHEngine.h"
using namespace Cephei::QL::Pricingengines::Vanilla;
#include <gen/QL/Models/Equity/GJRGARCHModel.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL::Models::Equity;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Vanilla::CAnalyticGJRGARCHEngine::CAnalyticGJRGARCHEngine (Cephei::QL::Models::Equity::IGJRGARCHModel^ model) : CPricingEngine(CAnalyticGJRGARCHEngine::typeid)
{
    CGJRGARCHModel^ _Cmodel;
    try
    {
#ifdef HANDLE
        _phAnalyticGJRGARCHEngine = NULL;
#endif
        _Cmodel = safe_cast<CGJRGARCHModel^> (model);
        _Cmodel->Lock();
        boost::shared_ptr<QuantLib::GJRGARCHModel>& _model = static_cast<boost::shared_ptr<QuantLib::GJRGARCHModel>&> (_Cmodel->GetShared ()); 
        _ppAnalyticGJRGARCHEngine = new boost::shared_ptr<QuantLib::AnalyticGJRGARCHEngine> (new QuantLib::AnalyticGJRGARCHEngine ( _model ));
        SetPricingEngine (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticGJRGARCHEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmodel != nullptr) _Cmodel->Unlock();
    }
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticGJRGARCHEngine::CAnalyticGJRGARCHEngine (boost::shared_ptr<QuantLib::AnalyticGJRGARCHEngine>& childNative, Object^ owner) : CPricingEngine(CAnalyticGJRGARCHEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticGJRGARCHEngine = NULL;
#endif
	_ppAnalyticGJRGARCHEngine = &childNative;
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticGJRGARCHEngine));
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticGJRGARCHEngine::CAnalyticGJRGARCHEngine (QuantLib::AnalyticGJRGARCHEngine& childNative, Object^ owner) : CPricingEngine(CAnalyticGJRGARCHEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticGJRGARCHEngine = NULL;
#endif
	_ppAnalyticGJRGARCHEngine = new boost::shared_ptr<QuantLib::AnalyticGJRGARCHEngine> (&childNative);
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticGJRGARCHEngine));
    _AnalyticGJRGARCHEngineOwner = owner;
    _PricingEngineOwner = owner;
}

Cephei::QL::Pricingengines::Vanilla::CAnalyticGJRGARCHEngine::CAnalyticGJRGARCHEngine (CAnalyticGJRGARCHEngine^ copy) : CPricingEngine(CAnalyticGJRGARCHEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticGJRGARCHEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAnalyticGJRGARCHEngine = new boost::shared_ptr<QuantLib::AnalyticGJRGARCHEngine> (copy->GetShared());
        _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticGJRGARCHEngine));
    }
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticGJRGARCHEngine::CAnalyticGJRGARCHEngine (System::Type^ t) : CPricingEngine(CAnalyticGJRGARCHEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticGJRGARCHEngine = NULL;
#endif
	if (!t->IsSubclassOf(CAnalyticGJRGARCHEngine::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Vanilla::CAnalyticGJRGARCHEngine::CAnalyticGJRGARCHEngine (QuantLib::Handle<QuantLib::AnalyticGJRGARCHEngine>& childNative, Object^ owner)  : CPricingEngine(CAnalyticGJRGARCHEngine::typeid)
{
	_phAnalyticGJRGARCHEngine = &childNative;
	_ppAnalyticGJRGARCHEngine = &static_cast<boost::shared_ptr<QuantLib::AnalyticGJRGARCHEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticGJRGARCHEngine));
    _AnalyticGJRGARCHEngineOwner = owner;
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticGJRGARCHEngine::CAnalyticGJRGARCHEngine (QuantLib::Handle<QuantLib::AnalyticGJRGARCHEngine> childNative)  : CPricingEngine(CAnalyticGJRGARCHEngine::typeid)
{
	_phAnalyticGJRGARCHEngine = &childNative;
	_ppAnalyticGJRGARCHEngine = &static_cast<boost::shared_ptr<QuantLib::AnalyticGJRGARCHEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticGJRGARCHEngine));
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Vanilla::CAnalyticGJRGARCHEngine::CAnalyticGJRGARCHEngine (QuantLib::AnalyticGJRGARCHEngine childNative)  : CPricingEngine(CAnalyticGJRGARCHEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticGJRGARCHEngine = NULL;
#endif
	_ppAnalyticGJRGARCHEngine = new boost::shared_ptr<QuantLib::AnalyticGJRGARCHEngine> (new QuantLib::AnalyticGJRGARCHEngine (childNative));
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticGJRGARCHEngine));
}
#endif

Cephei::QL::Pricingengines::Vanilla::CAnalyticGJRGARCHEngine::~CAnalyticGJRGARCHEngine ()
{
    if (_ppAnalyticGJRGARCHEngine != NULL)
    {
	    delete _ppAnalyticGJRGARCHEngine;
        _ppAnalyticGJRGARCHEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticGJRGARCHEngine::!CAnalyticGJRGARCHEngine ()
{
    if (_ppAnalyticGJRGARCHEngine != NULL)
    {
	    delete _ppAnalyticGJRGARCHEngine;
    }
}
QuantLib::AnalyticGJRGARCHEngine& Cephei::QL::Pricingengines::Vanilla::CAnalyticGJRGARCHEngine::GetReference ()
{
    if (_ppAnalyticGJRGARCHEngine == NULL) throw gcnew NativeNullException ();
	return **_ppAnalyticGJRGARCHEngine;
}
boost::shared_ptr<QuantLib::AnalyticGJRGARCHEngine>& Cephei::QL::Pricingengines::Vanilla::CAnalyticGJRGARCHEngine::GetShared ()
{
    if (_ppAnalyticGJRGARCHEngine == NULL) throw gcnew NativeNullException ();
	return *_ppAnalyticGJRGARCHEngine;
}
QuantLib::AnalyticGJRGARCHEngine* Cephei::QL::Pricingengines::Vanilla::CAnalyticGJRGARCHEngine::GetPointer ()
{
    if (_ppAnalyticGJRGARCHEngine == NULL) throw gcnew NativeNullException ();
	return &**_ppAnalyticGJRGARCHEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AnalyticGJRGARCHEngine>& Cephei::QL::Pricingengines::Vanilla::CAnalyticGJRGARCHEngine::GetHandle ()
{
	if (_phAnalyticGJRGARCHEngine == NULL)
	{
		_phAnalyticGJRGARCHEngine = new Handle<QuantLib::AnalyticGJRGARCHEngine> (*_ppAnalyticGJRGARCHEngine);
	}
	return *_phAnalyticGJRGARCHEngine;
}
#endif
bool Cephei::QL::Pricingengines::Vanilla::CAnalyticGJRGARCHEngine::HasNative () 
{
	return (_ppAnalyticGJRGARCHEngine != NULL);
}

Cephei::QL::Pricingengines::Vanilla::IAnalyticGJRGARCHEngine^ Cephei::QL::Pricingengines::Vanilla::CAnalyticGJRGARCHEngine::Calculate::get ()
{
    try
    {
    	(*_ppAnalyticGJRGARCHEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Vanilla::IAnalyticGJRGARCHEngine^ Cephei::QL::Pricingengines::Vanilla::CAnalyticGJRGARCHEngine_Factory::Create (Cephei::QL::Models::Equity::IGJRGARCHModel^ model)
{
    return gcnew CAnalyticGJRGARCHEngine ( model);
}
